package com.itheima.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.aom.pojo.AomUser;
import com.itheima.system.dao.UserDao;
import com.itheima.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public AomUser findUserNameAndPassWord(AomUser user) {
        //先从redis中查询是否有这个username
        if (!redisTemplate.opsForHash().hasKey("redisCount", user.getUsername())) {
            //如果不存在,就将count的值设为0
            int count = 0;
            redisTemplate.opsForHash().put("redisCount", user.getUsername(), count);
        }
        //将redisCount取出来
        int redisCount = (int) redisTemplate.boundHashOps("redisCount").get(user.getUsername());
        Example example = new Example(AomUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username", user.getUsername());
        criteria.andEqualTo("password", user.getPassword());

        AomUser aomUser = userDao.findByUserNameAndPssword(user);
        AomUser aomUsers = userDao.selectOneByExample(example);//登录的user
        if (aomUsers == null) {
            redisCount++;
            redisTemplate.opsForHash().put("redisCount", user.getUsername(), redisCount);
            //当用户输错五次的时候,就锁定他的账号,把状态值从1改成0,0:表示停用,1:表示启用
            if (redisCount >= 5) {
                aomUser.setStatus("0");
                userDao.updateByPrimaryKeySelective(aomUser);
            }
            throw new RuntimeException("用户名或者密码错误");
        }
        //如果用户成功
        redisTemplate.opsForHash().delete("redisCount", "username", redisCount);
        aomUser.setStatus("1");
        userDao.updateByPrimaryKeySelective(aomUser);

        return aomUsers;
    }

    @Override
    public void updatePassword(AomUser user) {
        AomUser aomUser = userDao.selectByPrimaryKey(user.getId());
        aomUser.setPassword(user.getPassword());
        userDao.updateByPrimaryKeySelective(aomUser);
    }

    @Override
    public void addUser(AomUser user,AomUser loginUser) {
        user.setPassword("123456");
        user.setAomid(loginUser.getAomid());
        user.setFirstlogin("0");
        user.setStatus("1");
        user.setParentid(loginUser.getId());
        userDao.insert(user);
    }

    @Override
    public List<AomUser> findAllUser(AomUser user) {
        List<AomUser> aomUsers = userDao.selectAll();
        return aomUsers;
    }

    @Override
    public AomUser findById(Integer id) {
        AomUser aomUser = userDao.selectByPrimaryKey(id);
        return aomUser;
    }

    @Override
    public void deleteById(Integer id) {
        userDao.deleteByPrimaryKey(id);
    }

    @Override
    public Page<AomUser> findPage(int page, int size,String username) {
        PageHelper.startPage(page, size);
        Example example = new Example(AomUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("username",username);
        List<AomUser> userList = userDao.selectByExample(example);
        return (Page<AomUser>) userList;
    }

    @Override
    public void updateUser(AomUser user) {
        userDao.updateByPrimaryKey(user);
    }

}